/**
 * Created by zhangxiaoyu on 16/12/16.
 */
angular.module('hyt.workbenchService', ['ngResource'])

  .factory('FeeInfo', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getFeeInfo,
      result;

    var resource = $resource(ApiUrl+':feeInfo',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getFeeInfo : function(feeInfo){
        return resource.query({
          feeInfo:feeInfo
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('FeeInfo.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('NoticeList', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getNotice,
      noticeData={},pageSize=10;

    var resource = $resource(ApiUrl+':noticeSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getNoticeData : function(noticeSearch){
        var hasNextPage = true;
        resource.query({
          noticeSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            userMsg:noticeSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            noticeData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Notice.Updated');
        })
      },
      getResult:function(){
        if(noticeData[pageSize]===undefined){
          return false;
        }
        return noticeData[pageSize].data;
      },
      getMoreNoticeData : function(noticeSearch){
        if(noticeData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = noticeData[pageSize].hasNextPage;
        var nextPage = noticeData[pageSize].nextPage;
        var moreNoticeData = noticeData[pageSize].data;
        resource.query({
          noticeSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            userMsg:noticeSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreNoticeData = moreNoticeData.concat(r.rows);

          noticeData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreNoticeData
          };
          $rootScope.$broadcast('Notice.Updated');
        })
      },
      hasNextPage: function() {
        if (noticeData[pageSize] === undefined) {
          return false;
        }
        return noticeData[pageSize].hasNextPage;
      }
    }
  })
